﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Netium.Compiler
{
    public class SymbolTableStack : Netium.Compiler.ISymbolTableStack
    {
        private Stack<ISymbolTable> _stack = new Stack<ISymbolTable>();

        public SymbolTableStack()
        {
        }

        public void Push(ISymbolTable symbolTable)
        {
            Debug.Assert(!_stack.Contains(symbolTable));

            _stack.Push(symbolTable);
        }

        public ISymbolTable Pop()
        {
            return _stack.Pop();
        }

        public ISymbolTable Peek()
        {
            return _stack.Peek();
        }

        public ISymbol LookupLocal(string symbolName)
        {
            return _stack.Peek().Lookup(symbolName);
        }

        public ISymbol LookupGlobal(string symbolName)
        {
            var symbol = LookupLocal(symbolName);
            if (symbol == null)
                symbol = _stack.Last().Lookup(symbolName);

            return symbol;
        }
    }
}
